package com.warehouse.module.system.controller.admin.appuser;

import com.warehouse.module.system.controller.admin.user.vo.user.UserSimpleRespVO;
import com.warehouse.module.system.dal.dataobject.user.AdminUserDO;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.annotation.security.PermitAll;
import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import com.warehouse.framework.common.pojo.PageParam;
import com.warehouse.framework.common.pojo.PageResult;
import com.warehouse.framework.common.pojo.CommonResult;
import com.warehouse.framework.common.util.object.BeanUtils;
import static com.warehouse.framework.common.pojo.CommonResult.success;

import com.warehouse.framework.excel.core.util.ExcelUtils;

import com.warehouse.framework.apilog.core.annotation.ApiAccessLog;
import static com.warehouse.framework.apilog.core.enums.OperateTypeEnum.*;

import com.warehouse.module.system.controller.admin.appuser.vo.*;
import com.warehouse.module.system.dal.dataobject.appuser.AppUserDO;
import com.warehouse.module.system.service.appuser.AppUserService;

@Tag(name = "管理后台 - APP用户")
@RestController
@RequestMapping("/system/app-user")
@Validated
public class AppUserController {

    @Resource
    private AppUserService appUserService;

    @PostMapping("/create")
    @Operation(summary = "创建APP用户")
    @PreAuthorize("@ss.hasPermission('system:app-user:create')")
    public CommonResult<Long> createAppUser(@Valid @RequestBody AppUserSaveReqVO createReqVO) {
        return success(appUserService.createAppUser(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新APP用户")
    @PreAuthorize("@ss.hasPermission('system:app-user:update')")
    public CommonResult<Boolean> updateAppUser(@Valid @RequestBody AppUserSaveReqVO updateReqVO) {
        appUserService.updateAppUser(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除APP用户")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('system:app-user:delete')")
    public CommonResult<Boolean> deleteAppUser(@RequestParam("id") Long id) {
        appUserService.deleteAppUser(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得APP用户")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('system:app-user:query')")
    public CommonResult<AppUserRespVO> getAppUser(@RequestParam("id") Long id) {
        AppUserDO appUser = appUserService.getAppUser(id);
        return success(BeanUtils.toBean(appUser, AppUserRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得APP用户分页")
    @PreAuthorize("@ss.hasPermission('system:app-user:query')")
    public CommonResult<PageResult<AppUserRespVO>> getAppUserPage(@Valid AppUserPageReqVO pageReqVO) {
        PageResult<AppUserDO> pageResult = appUserService.getAppUserPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, AppUserRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出APP用户 Excel")
    @PreAuthorize("@ss.hasPermission('system:app-user:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportAppUserExcel(@Valid AppUserPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<AppUserDO> list = appUserService.getAppUserPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "APP用户.xls", "数据", AppUserRespVO.class,
                        BeanUtils.toBean(list, AppUserRespVO.class));
    }

    @GetMapping("/listUsers/{companyId}")
    @PermitAll
    public CommonResult<List<AppUserDO>> listUsers(@PathVariable("companyId") Long companyId){
        return success(appUserService.listCompanyUsers(companyId));
    }

}